package edu.gdkm.sql.dao;

import edu.gdkm.sql.pojo.Product;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ProductMapper {

    // 查询全部商品
    @Select("SELECT * FROM product")
    @Results(id = "productMap",value = {
            @Result(property = "typeid", column = "typeid"),
            @Result(property = "category", column = "typeid",
                    one = @One(select = "edu.gdkm.sql.dao.CategoryMapper.findById"))
    })
    List<Product> findAll();


    @Select("SELECT * FROM product WHERE typeid = #{typeid}")
    @ResultMap("productMap")
    List<Product> findByTypeid();


    // 根据id查询商品
    @Select("SELECT * FROM product WHERE id = #{id}")
    @ResultMap("productMap")
    Product findById(int id);
    @Select("SELECT * FROM product WHERE goodsname = #{goodsname}")
    @ResultMap("productMap")
    Product findByGoodsname(String goodsname);

    @Insert("INSERT INTO product(goodsname, price, typeid) VALUES(#{goodsname}, #{price}, #{typeid})")
    int insertProduct(Product product);

    // 更新商品
    @Update("UPDATE product SET goodsname=#{goodsname}, price=#{price}, typeid=#{typeid} WHERE id=#{id}")
    int updateProductById(Product product);

    // 删除商品
    @Delete("DELETE FROM product WHERE id=#{id}")
    int deleteProductById(int id);
    @Select("SELECT * FROM product WHERE goodsname LIKE CONCAT('%', #{goodsname}, '%') AND price < #{price}")
    @ResultMap("productMap")
    List<Product> findByGoodsnameAndPrice(@Param("goodsname") String goodsname, @Param("price") double price);
}